/**
 * User: c3t
 * Date: 14-3-25
 * Time: 下午4:16
 */


define(function (require, exports, module) {
    return function (jQuery) {
        (function ($) {
            var setting = null;
            $.fn.keywordInput = function (opts) {
                setting = $.extend({
                    number: 5, //关键字数量限定
                    msg: "请输入关键字,通过逗号确认",
                    as: {
                        enable: true,
                        url: "#",
                        minLength:1
                    }
                }, opts || {});
                init(this);

                //以下autocomplete必须加载好jquery-ui.js
                if(setting.as.enable) {
                   this.autocomplete({
                        source:setting.as.url,
                        minLength:setting.as.minLength
                    });
                }

                this.keyup(inputKeywords);
                //通过事件委派。对新加入的标签添加事件
                $("#keywords-wrap").on("click", ".keyword-shut", function (event) {
                    $(this).parent(".keyword-in").remove();
                    event.preventDefault();
                });

            };
            function init(input) {
                $(input).val(setting.msg);
                $(input).addClass("keyword-input");
                $(input).wrap("<div id='keyword-container'></div>").before("<div id='keywords-wrap'></div>");

                $(input).focus(function () {
                    $(this).val("");
                });

                $(input).blur(function () {
                    $(this).val(setting.msg);
                });
            }

            function inputKeywords(event) {
                var code = event.keyCode;
                if (code == 188 || code == 13) {
                    var c = $(this).val();
                    if (c != "") {
                        if ($(".keyword-in").length >= setting.number) {
                            alert("最多只能允许添加" + setting.number + "个关键字");
                            event.preventDefault();
                            return  false;
                        }
                        //防止关键字重复
                        var aks = $("input[name='aks']");
                        for (var i = 0; i < aks.length; i++) {
                            if ($(aks[i]).val() == c) {
                                alert("不能添加重复的关键字");
                                event.preventDefault();
                                return false;
                            }
                        }

                        var ki = createKeyWord(c);
                        $('#keywords-wrap').append(ki);
                        $(this).val("");
                    }
                }

            }

            function createKeyWord(val) {
                return "<div class='keyword-in'>" +
                    "<span>" + val + "</span>" +
                    "<a href='#' class='keyword-shut'>×</a>" +
                    "<input type='hidden' name='aks' value='" + val + "'/>" +
                    "</div>";
            }
        })(jQuery);
    }
});